package swing;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

public class Text{
	File f=null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Text frame = new Text();										
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public Text() {
		f=new File("d:/text.txt");
		JFrame frame=new JFrame("记事本");
		JTextArea text=new JTextArea();
		text.setEditable(true);
		frame.getContentPane().add(new JScrollPane(text));
		JMenu menuFile=new JMenu("操作");
		JMenuBar menuBar=new JMenuBar();
		JMenuItem openitem=new JMenuItem("打开");
		JMenuItem saveitem=new JMenuItem("保存");
		openitem.setMnemonic('O') ;						// 设置快捷键O
		saveitem.setMnemonic('S') ;					// 设置快捷键S
		openitem.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK));
		saveitem.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK));		
		menuFile.add(openitem);
		menuFile.addSeparator();
		menuFile.add(saveitem);
		menuBar.add(menuFile);
		frame.setJMenuBar(menuBar);
		frame.setSize(474, 315);
		frame.setLocation(300, 200);
		frame.setVisible(true);
		openitem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				char c1[]=new char[1024];
				int i=0;
				try {
					JFileChooser c = new JFileChooser();
					int r = c.showOpenDialog(null);
					if( r == JFileChooser.APPROVE_OPTION ){
						f = c.getSelectedFile(); 
						if( ! f.exists() ){
							f.createNewFile();
						}
						System.out.println(f.getAbsolutePath());
						FileReader fr=new FileReader(f);
						do{
						i=fr.read(c1);
						text.setText(new String(c1, 0, i));
						}while(i==0);
						fr.close();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		saveitem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					if( f == null || ! f.exists() ){
						JFileChooser c = new JFileChooser();
						int r = c.showSaveDialog(null);
						if( r == JFileChooser.APPROVE_OPTION ){
							f = c.getSelectedFile();
							if( ! f.exists() ){
								f.createNewFile();
							}
						} else {
							return;
						}
					}
					FileWriter fw = new FileWriter(f);
					fw.write(text.getText());
					fw.flush();
					fw.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}

			}
		});
	}
	

}
